package com.ty.zls.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qmuiteam.qmui.layout.QMUILinearLayout;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.ty.zls.R;
import com.ty.zls.activity.PrinterSettingActivity;
import com.ty.zls.bean.Account;
import com.ty.zls.bean.PrintWater;
import com.ty.zls.bean.Water;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WaterRecordAdapter extends RecyclerView.Adapter<WaterRecordAdapter.ViewHolder> {

    private int total;
    private List<Water> datas;
    private Context context;
    private Account account;
    private int pageNum = 1;
    private int pageSize = 5;
    private Map<String,String> status = new HashMap<>();

    public WaterRecordAdapter(List<Water> datas, Context context,Account account) {
        super();
        this.datas = datas;
        this.context = context;
        this.account = account;
        status.put("0","正常");
        status.put("3","撤销");
        status.put("4","减免审核");
        status.put("5","抄表审核");
        status.put("6","减免审核未通过");
        status.put("7","抄表审核未通过");
    }

    @NonNull
    @Override
    public WaterRecordAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        QMUILinearLayout root = (QMUILinearLayout) inflater.inflate(R.layout.water_record_item, viewGroup, false);
        root.setRadiusAndShadow(QMUIDisplayHelper.dp2px(context, 15),
                QMUIDisplayHelper.dp2px(context, 14), 0.25f);
        return new ViewHolder(root);
    }

    @Override
    public void onBindViewHolder(@NonNull WaterRecordAdapter.ViewHolder viewHolder, int i) {
        Water water = datas.get(i);
        viewHolder.setAmout(water.getAmount());
        viewHolder.setEndScale(water.getScale());
        viewHolder.setEndTime(water.getEndTime());
        viewHolder.setLastScale(water.getLastScale());
        viewHolder.setLasttime(water.getLastTime());
        viewHolder.getWater_record_print().setTag(i);
        viewHolder.setStatus(water.getStatus());
    }


    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView record_lasttime_tv;
        private TextView record_endtime_tv;
        private TextView record_lastscale_tv;
        private TextView record_endscale_tv;
        private TextView record_amount_tv;
        private TextView record_status_tv;


        private QMUIRoundButton water_record_print;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            record_lasttime_tv = itemView.findViewById(R.id.record_lasttime_tv);
            record_endtime_tv = itemView.findViewById(R.id.record_endtime_tv);
            record_lastscale_tv = itemView.findViewById(R.id.record_lastscale_tv);
            record_endscale_tv = itemView.findViewById(R.id.record_endscale_tv);
            record_amount_tv = itemView.findViewById(R.id.record_amount_tv);
            water_record_print = itemView.findViewById(R.id.water_record_print);
            record_status_tv = itemView.findViewById(R.id.record_status_tv);
            /* 打印历史抄表记录 */
            water_record_print.setOnClickListener(this);
        }

        public void setStatus(String sta){
            record_status_tv.setText(status.get(sta));
        }

        public void  setLasttime(String lasttime){
            record_lasttime_tv.setText(lasttime);
        }

        public void setEndTime(String endTime){
            record_endtime_tv.setText(endTime);
        }

        public void setLastScale(String lastScale){
            record_lastscale_tv.setText(lastScale);
        }

        public void setEndScale(String endScale){
            record_endscale_tv.setText(endScale);
        }

        public void setAmout(String amount){
            record_amount_tv.setText(amount);
        }

        public QMUIRoundButton getWater_record_print() {
            return water_record_print;
        }

        @Override
        public void onClick(View v) {
            int tag = (int) v.getTag();
            Water water = datas.get(tag);
            PrintWater printWater = new PrintWater(account.getId(), account.getName(), account.getArea(), water.getLastScale()
                    , water.getLastTime(), water.getEndTime(), water.getScale(), account.getBalance(),
                    account.getArreage(), water.getCreateBy());
            Intent intent = new Intent(context, PrinterSettingActivity.class);
            intent.putExtra("entity",printWater);
            context.startActivity(intent);
        }
    }


    public List<Water> getDatas() {
        return datas;
    }

    public void setDatas(List<Water> datas) {
        this.datas = datas;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

}
